<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods && goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <Goodsimage :images="goods.mainPictures"></Goodsimage>
          <GoodsSale></GoodsSale>
        </div>
        <div class="spec">
          <GoodsName :goods="goods"></GoodsName>
          <!-- sku组件 -->
          <GoodsSku :goods="goods" @change="changesku"></GoodsSku>
          <!-- 数量选择组件 -->
          <XtxNumbox
            v-model="num"
            :max="goods.inventory"
            :label="数量"
          ></XtxNumbox>
          <!-- 按钮组件 -->
          <XtxButton
            type="primary"
            style="margin-top: 20px"
            @click="insertcart()"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodTables></GoodTables>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn></GoodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { nextTick, ref, watch, provide, getCurrentInstance } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import GoodsRelevant from './components/goods-relevant'
import { findGoods } from '@/apis/product'
import Goodsimage from './components/goods-image.vue'
import GoodsSale from './components/goods-sale.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodTables from './components/goods-tables.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    Goodsimage,
    GoodsSale,
    GoodsName,
    GoodsSku,
    GoodTables,
    GoodsHot,
    GoodsWarn
  },
  setup () {
    const goods = usegoods()
    const changesku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      carts.value = sku
    }
    // 选择的数据
    const num = ref(1)
    provide('goods', goods)

    // 添加购物车
    const store = useStore()
    const carts = ref(null)
    const { proxy } = getCurrentInstance()
    const insertcart = () => {
      if (carts.value && carts.value.skuId) {
        // id skuId name attrsText picture price nowPrice selected stock count isEffective
        const { skuId, specsText: attrsText, inventory: stock } = carts.value
        const { id, name, price, mainPictures } = goods.value
        store
          .dispatch('cart/insertCart', {
            skuId,
            attrsText,
            stock,
            id,
            name,
            price,
            nowPrice: price,
            picture: mainPictures[0],
            selected: true,
            isEffective: true,
            count: num.value
          })
          .then(() => {
            proxy.$message({ type: 'success', text: '加入购物车成功' })
          })
      } else {
        proxy.$message({ text: '请选择完整规格' })
      }
    }
    return { goods, changesku, num, insertcart }
  }
}
const usegoods = () => {
  const route = useRoute()
  const goods = ref(null)
  watch(
    () => route.params.id,
    (newvalue) => {
      if (newvalue && route.path === `/product/${newvalue}`) {
        findGoods(route.params.id).then((res) => {
          // 这两句代码配合v-if可以让组件中的内容重新销毁创建，达到更新数据的目的
          goods.value = null
          nextTick(() => {
            goods.value = res.result
          })
        })
      }
    },
    { immediate: true }
  )
  return goods
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
